<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="name">
              <el-input v-model="filters.name" clearable placeholder="小区名称"></el-input>
            </el-form-item>
            <el-form-item prop="streetIds">
              <Region :streetValues="filters.streetIds" @regionChange="handleItemChange" :isChangeOnSelect="true"></Region>
            </el-form-item>
            <el-form-item prop="payState">
              <el-select v-model="filters.applyState" placeholder="审核状态" clearable>
                <el-option v-for="item in applyStateList" :value="item.value" :label="item.label" :key="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <!--<el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>-->
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="申请小区" prop="villageName"> </el-table-column>
        <el-table-column align="center" label="小区地址" prop="address"> </el-table-column>
        <el-table-column align="center" width="100" label="申请人" prop="linkMan"> </el-table-column>
        <el-table-column align="center" width="100" label="申请人电话" prop="linkPhone"> </el-table-column>
        <el-table-column align="center" width="300" label="申请说明" prop="applyDesc"> </el-table-column>
        <el-table-column align="center" width="200" label="申请时间" prop="createDate"> </el-table-column>
        <el-table-column label="审核状态" align="center" width="100px">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" effect="dark" :type="formatApplyStates(scope.row.applyState)">
                {{ formatApplyState(scope.row.applyState) }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionSee" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <!--<el-dropdown trigger="click" size="small">
              <el-button type="primary">
                <i class="fa fa-list-ul"></i>更多
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>-->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import VillageApplyApi from '@/api/address/villageApply';
import Region from '@/components/Region';
export default {
  name: 'VillageApplyManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        name: '',
        applyState: '',
        streetIds: [],
        provinceId: '',
        cityId: '',
        areaId: '',
        streetId: '',
      },
      applyStateList: [
        { value: 1, label: '待审核' },
        { value: 2, label: '审核通过' },
        { value: 3, label: '审核不通过' },
      ],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionCheck: 'SYSTEM_MANAGER_ADDRESS_VILLAGE_APPLY_LIST_CHECK',
      permissionSee: 'SYSTEM_MANAGER_ADDRESS_VILLAGE_APPLY_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_ADDRESS_VILLAGE_APPLY_LIST_EDIT',
    };
  },
  components: {
    Region,
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },

    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/address/villageApplyDetail',
        query: { id: row.villageApplyId },
      });
    },
    formatApplyState(cellValue) {
      if (cellValue === 1) {
        return '待审核';
      } else if (cellValue === 2) {
        return '审核通过';
      } else if (cellValue === 3) {
        return '审核不通过';
      }
    },
    formatApplyStates(cellValue) {
      if (cellValue === 1) {
        return 'info';
      } else if (cellValue === 2) {
        return 'success';
      } else if (cellValue === 3) {
        return 'danger';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.filters.provinceId = '';
      this.filters.cityId = '';
      this.filters.areaId = '';
      this.filters.streetId = '';
      this.filters.streetIds = [];
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    handleItemChange(val) {
      this.filters.provinceId = val.provinceId;
      this.filters.cityId = val.cityId;
      this.filters.areaId = val.areaId;
      this.filters.streetId = val.streetId;
      if (val.provinceId) {
        this.filters.streetIds = [val.provinceId, val.cityId, val.areaId, val.streetId];
      } else {
        this.filters.streetIds = [];
      }
    },
    listData() {
      // 获取列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
      };
      cond.name = _this.filters.name;
      cond.applyState = _this.filters.applyState;
      cond.provinceId = _this.filters.provinceId;
      cond.cityId = _this.filters.cityId;
      cond.areaId = _this.filters.areaId;
      cond.streetId = _this.filters.streetId;
      VillageApplyApi.listVillageApply(cond)
        .then((res) => {
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
          _this.listLoading = false;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
